<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
	Object obj = request.getSession().getAttribute("role_resource");
	if (null == obj) {
		response.setStatus(1000);
		return;
	}
%>
<script type="text/javascript">
	//外包商下拉列表
	var epiboly_store = new Ext.data.JsonStore({
		root : "data",
		fields : [ "id", "name" ],
		url : "epiboly!listAll.action"
	});

	//搜索条件外包商下拉列表
	var epiboly_store1 = new Ext.data.JsonStore({
		root : "data",
		fields : [ "id", "name" ],
		url : "epiboly!listAll.action",
		listeners : {
			"load" : function() {
				var RecordName = Ext.data.Record.create([ {
					name : "id"
				}, {
					name : "name"
				} ]);
				var newRecord = new RecordName({
					id : "",
					name : "全部"
				});
				this.insert(0, newRecord);
			}
		}
	});

	//插槽列表数据源
	var data_store = new Ext.data.JsonStore({
		root : "data",
		totalProperty : "total",
		fields : [ "foldoutid", "epibolyno", "foldoutname", "foldoutpri",
				"epibolyname" ],
		url : "foldOut!list.action"
	});

	//定义数据显示面板
	var p1 = {
		id : "p1",
		xtype: "grid",
		stripeRows:true,
		loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
		autoExpandColumn: "epibolyname",
		autoExpandMin : 100,//自动填充最小宽度
		store : data_store,
		headersDisabled: true,
		columns : [ new Ext.grid.RowNumberer({
			header : "序号",
			width : 35
		}), {
			header : "外包商名称",
			dataIndex : "epibolyname",
			id : "epibolyname", 
			menuDisabled: true
		}, {
			header : "插槽编号",
			dataIndex : "foldoutid", 
			menuDisabled: true
		}, {
			header : "插槽名称",
			dataIndex : "foldoutname", 
			menuDisabled: true
		}, {
			header : "优先级",
			dataIndex : "foldoutpri", 
			menuDisabled: true
		} ],
		buttonAlign : "center",
		bbar : new Ext.ux.PagingToolbar({
			id : "foldoutPaging",
			pageSize : 19,
			store : data_store
		}),
		buttons : [ {
			text : "新增",
			handler : function() {
				oper_type = "add";
				Ext.getCmp("p2").resetParam();
				Ext.getCmp("mycontainer").layout.setActiveItem(1);
			}
		}, {
			text : "修改",
			handler : function() {
				var record = Ext.getCmp("p1").getSelectionModel().getSelected();
				if (record){
					Ext.getCmp("p2").setParam(record.data);
					oper_type = "upd";
					Ext.getCmp("mycontainer").layout.setActiveItem(1);
				} else {
					Ext.Msg.show({
						title : "警告",
						msg : "没有任何行被选中，无法进行修改操作!",
						buttons : Ext.Msg.OK,
						animEl : "elId",
						icon : Ext.MessageBox.WARNING
					});
				}
			}
		}, {
			text : "删除",
			handler : function() {
				var record = Ext.getCmp("p1").getSelectionModel().getSelected();
				if (record) {
					Ext.Msg.confirm("警告", "确定要删除吗?", function(button) {
						if (button == "yes") {
							var url = "foldOut!del.action";
							var params = {
								"foldOut.foldoutid" : record.data.foldoutid,
								"foldOut.epibolyno" : record.data.epibolyno,
								"foldOut.foldoutpri" : record.data.foldoutpri
							};
							oper(url, params);
						}
					});
				} else {
					Ext.Msg.show({
						title : "警告",
						msg : "没有任何行被选中，无法进行删除操作!",
						buttons : Ext.Msg.OK,
						animEl : "elId",
						icon : Ext.MessageBox.WARNING
					});
				}
			}
		} ],
		tbar : [
				"&nbsp&nbsp外包商:",
				{
					id : "a",
					xtype : "billCombox",
					displayField : "name",
					store : epiboly_store1,
					width:120,
					listeners : {
						"select" : function() {
							data_store.baseParams = data_store.baseParams || {};

							data_store.baseParams["foldOut.epibolyno"] = Ext
									.getCmp("a").getValue();

							data_store.load({
								params : {
									start : 0,
									limit : 19
								}
							});

						}
					}
				} ]
	};

	var p2 = {
		id : "p2",
		labelWidth : 85,
		xtype : "form",
		bodyStyle : "padding:5px 5px 0",
		defaultType : "textfield",
		items : [ {
			id : "foldOut.epibolyno",
			fieldLabel : "外包商名称",
			name : "foldOut.epibolyno",
			xtype : "billCombox",
			store : epiboly_store,
			valueField : "id",
			displayField : "name",
			anchor : "100%"
		}, {
			id : "foldOut.foldoutid",
			fieldLabel : "插页纸槽编号",
			allowBlank : false,
			xtype : "numberfield",
			maxLength: 2,
			allowNegative: false,
			name : "foldOut.foldoutid",
			anchor : "100%"
		}, {
			id : "foldOut.foldoutname",
			fieldLabel : "插页纸槽名称",
			vtype : "alphanumchinese",
			name : "foldOut.foldoutname",
			anchor : "100%",
			validator : function(val) {
				if (val == "")
					return "该输入项为必输项！";
				if (Ext.fucCheckCHLength(val) > 20) {
					return "请输入20个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
				}
				return true;
			}
		}, {
			id : "foldOut.foldoutpri",
			xtype : "numberfield",
			maxLength: 2,
			fieldLabel : "优先级",
			name : "foldOut.foldoutpri",
			anchor : "100%",
			allowNegative: false,
			validator: function(val){
				if(!(/^[\d]{1,2}$/).test(val) || parseInt(val) < 1 || parseInt(val) > 99) {
					return "必须设置为1到99之间的整数！";
				}
			}
		} ],
		buttonAlign : "center",
		buttons : [ {
			text : "保存",
			id:"save", 
			handler : function(e) {
				if (!Ext.getCmp("p2").getForm().isValid()) {
					//Ext.Msg.alert("提示","验证不通过！");
					Ext.Msg.show({
						title : "警告",
						msg : "验证不通过!",
						buttons : Ext.Msg.OK,
						animEl : "elId",
						icon : Ext.MessageBox.WARNING
					});
					return;
				}
				var form = this.ownerCt.ownerCt.getForm();
				var foldoutid = Ext.getCmp("foldOut.foldoutid").getValue();
				var foldoutname = Ext.getCmp("foldOut.foldoutname").getValue();
				var epibolyno = Ext.getCmp("foldOut.epibolyno").getValue();
				var foldoutpri = Ext.getCmp("foldOut.foldoutpri").getValue();
				var url, params;
				if (oper_type == "add") {
					url = "foldOut!add.action";
				} else if (oper_type == "upd") {
					url = "foldOut!upd.action";
				}
				params = {
					"foldOut.foldoutid" : foldoutid,
					"foldOut.foldoutname" : foldoutname,
					"foldOut.epibolyno" : epibolyno,
					"foldOut.foldoutpri" : foldoutpri
				};
				e.setDisabled(true);
				oper(url, params);
			}
		}, {
			text : "取消",
			handler : function() {
				Ext.getCmp("mycontainer").layout.setActiveItem(0);
			}
		} ],
		setParam : function(data) {
			var p2 = this;
			p2.getComponent("foldOut.foldoutid").setValue(data.foldoutid);
			p2.getComponent("foldOut.foldoutid").setDisabled(true);
			p2.getComponent("foldOut.epibolyno").setValue(data.epibolyno);
			p2.getComponent("foldOut.epibolyno").setDisabled(true);
			p2.getComponent("foldOut.foldoutname").setValue(data.foldoutname);
			p2.getComponent("foldOut.foldoutpri").setValue(data.foldoutpri);
		},
		resetParam : function() {
			var p2 = this;
			p2.getComponent("foldOut.foldoutid").setValue("");
			p2.getComponent("foldOut.foldoutid").setDisabled(false);
			p2.getComponent("foldOut.epibolyno").setValue("");
			p2.getComponent("foldOut.epibolyno").setDisabled(false);
			p2.getComponent("foldOut.foldoutname").setValue("");
			p2.getComponent("foldOut.foldoutpri").setValue("");
		}
	};

	var foldout_pel = new Ext.Panel({
		id : "mycontainer",
		border : false,
		layout : "card",
		activeItem : 0,
		items : [ p1, p2 ]
	});

	data_store.load({
		params : {
			start : 0,
			limit : 19
		}
	});
	epiboly_store.load();
	epiboly_store1.load({
		callback : function() {
			Ext.getCmp("a").setValue("");
		}
	});

	var oper = function(url, params) {
		Ext.Ajax.request({
			clientValidation : true,
			params : params,
			url : url,
			success : function(response, opts) {
				var d = eval("(" + response.responseText + ")");
				if (d.code == "1") {
					Ext.getCmp("p1").getBottomToolbar().doRefresh();
					Ext.getCmp("foldoutPaging").doRefresh();
					//Ext.Msg.alert("成功",d.data);
					Ext.Msg.show({
						title : "提示",
						msg : "操作成功!",
						buttons : Ext.Msg.OK,
						icon : Ext.MessageBox.INFO
					});
					Ext.getCmp("mycontainer").layout.setActiveItem(0);
				} else if (d.code == "2") {
					Ext.Msg.show({
						title : "警告",
						msg : "您输入的插槽编号或优先级有重复!",
						buttons : Ext.Msg.OK,
						animEl : "elId",
						icon : Ext.MessageBox.WARNING
					});
				} else {
					Ext.Msg.show({
						title : "提示",
						msg : "操作失败!",
						buttons : Ext.Msg.OK,
						icon : Ext.MessageBox.ERROR
					});
				}
				
				Ext.getCmp("save").setDisabled(false);
			}
		});
	};

	var CenterPanel = Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(foldout_pel);
	CenterPanel.doLayout();
</script>

